package cn.com.generaldata.os.service.common;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.com.generaldata.jsme_indicator_system.entity.ReportTypeNz;
import cn.com.generaldata.jsme_indicator_system.entity.StatSystem;
import cn.com.generaldata.os.util.FileUtil;

import com.google.common.collect.Maps;

@Service
public class StatSystemService {

	@Autowired
	private SqlTemplate sqlTemplate;
	
	@Autowired
	private Dao dao;

	public Map<String, String> getStatSystemMap() {
		Map<String, String> re = Maps.newLinkedHashMap();

		List<StatSystem> statSystems = dao.query(StatSystem.class, Cnd.orderBy().asc("no"));

		for (StatSystem statSystem : statSystems) {
			re.put(statSystem.getId().toString(), statSystem.getName());
		}
		return re;
	}

	public StatSystem getStatSystemByName(String name) {
		return dao.fetch(StatSystem.class, Cnd.where("name", "=", name));
	}
	
	public List<StatSystem> getStatSystem(String userId, String roleId){
		String sql = "select *  from T_STAT_SYSTEM where id in (select rt.stat_system  from TBL_USER_INDUSTRY ui "
              + " inner join TBL_DUSTRY_REPORT ir on ir.industry_id = ui.industry_id inner join Tbl_Report_Type rt "
              + " on rt.id = ir.report_type_id where ui.user_id = @userId) ";
		
		Map params = new HashMap();
		
		
		String roleSql = "select * from t_report_type_unit_type where report_unit_type = '" + roleId + "'";
		Record record = sqlTemplate.queryForRecord(roleSql, null, null);;
		
		if (record!=null){
			sql = "select *  from T_STAT_SYSTEM where id in (select rt.stat_system from  Tbl_Report_Type rt" +
					" inner join t_report_type_unit_type tut  on tut.report_unit_type = @roleId   and tut.report_type_id = rt.id )";
			params.put("roleId", roleId);
		}else{
			params.put("userId", userId);
		}

		/*******查询重点涉海直报企业报表类型*******/
		sql = sql + " union select s.*  from t_unit_nation n " 
				+ " left join tbl_unittype_report ut on (n.hy_code = ut.type_code or n.unit_type = ut.unit_type) "
				+ " left join tbl_report_type type on type.id = ut.report_type_id "
				+ " left join t_stat_system s on s.id = type.stat_system left join tbl_unit un on un.id = n.unit_id "
				+ " left join tbl_user u on u.user_unit = un.id  where u.id = @userId ";
		
		params.put("userId", userId);
		
		List<Record> list = sqlTemplate.queryRecords(sql, null, params);
		
		List<StatSystem> sysList = new ArrayList<StatSystem>();
		for (Record re :  list){
			StatSystem sys = new StatSystem();
			sys.setId(re.getInt("id"));
			sys.setName(re.getString("name"));
			sysList.add(sys);
		}
		
		return sysList;
    }
	
	public void recoverTypeTxt() {
		String path="D:\\template\\2018新报表模板\\";
		File dirs = new File(path);
		File[] listFiles = dirs.listFiles();
		for(File dir:listFiles)
		{
			if(dir.isDirectory()){
				ReportTypeNz reportTypeNz = dao.fetch(ReportTypeNz.class,dir.getName());
				File[] listDetail = dir.listFiles();
				for(File detail:listDetail){
					if("report_info.txt".equals(detail.getName())){
						try {
							String reportInfo = FileUtil.readTxtFile(detail);
							reportInfo=reportInfo.replaceFirst("null", "");
							reportTypeNz.setReportInfo(reportInfo);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else if("template.txt".equals(detail.getName())){
						try {
							String template = FileUtil.readTxtFile(detail);
							template=template.replaceFirst("null", "");
							reportTypeNz.setTemplate(template);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				dao.update(reportTypeNz);
			}
		}
		
	}
}
